<template>
  <div ref="container" class="productList3">
    <div class="header">
      <span @click="toCart">购物车</span>
      <img @click="toCart" src="@assets/images/card_block.png" alt="" srcset="" />
    </div>
    <newGoodsList :type="2"></newGoodsList>
    <!-- <div
      class="noCommodity"
      style="background-color:#fff;"
      v-if="collectProductList.length < 1 && page == 1"
    > -->
      <!-- <div class="noPictrue">
        <img src="@assets/images/noCollection.png" class="image" />
        <p class="txt">还没有关注,赶快去关注一波吧~</p>
      </div> -->
      <!-- <div class="noCart" v-if="collectProductList.length < 1 && page >= 1">
        <div class="pictrue"><img src="@assets/images/noOrder.png" /></div>
        <p class="txt">还没有关注,赶快去关注一波吧~</p>
      </div> -->
      <!-- <Recommend></Recommend> -->
    <!-- </div> -->
  </div>
</template>
<script>
// import Recommend from "@components/Recommend";
import { getCollectUser, getCollectDel } from "@api/user";
import newGoodsList from "@components/newGoodsList";
import Loading from "@components/Loading";
import cookie from "@utils/store/cookie";
import { clearAuthStatus } from "@libs/wechat";
import dayjs from "dayjs";
export default {
  name: "GoodsCollection",
  components: {
    // Recommend,
    Loading,
    newGoodsList
  },
  props: {},
  data: function() {
    return {
      page: 1,
      limit: 20,
      collectProductList: [],
      loadTitle: "",
      loading: false,
      loadend: false,
      from1:'',
    };
  },
  mounted: function() {
    var that = this;
    let isFromApp = this.$route.query.from1;
    if(isFromApp == undefined){
      isFromApp = 'h5'
    }
    this.from1 = isFromApp;
    cookie.set("from1", isFromApp);
    // this.get_user_collect_product();
    if(isFromApp == 'app'){
        if(cookie.get('from1') == '' || cookie.get('from1') == undefined){
          cookie.set('from1','app');
        }
        this.getParameter();
        this.isNo();
        window['onWebResponse']=(str)=>{
          that.onWebResponse(str)
        }
      }
  },
  methods: {
    //声音禁掉
    isNo:function(){
        var u = navigator.userAgent, app = navigator.appVersion;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
        var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        const win = window;
        var obj={
            "action": "onClickPlay",
            "content": {}
        }
        if (isAndroid) {
            App.onAppResponse(JSON.stringify(obj));
        }
        if (isIOS) {
            win.webkit.messageHandlers.onAppResponse.postMessage(JSON.stringify(obj));
        }  
    },
    get_user_collect_product: function() {
      // let that = this;
      // if (that.loading) return; //阻止下次请求（false可以进行请求）；
      // if (that.loadend) return; //阻止结束当前请求（false可以进行请求）；
      // that.loading = true;
      // getCollectUser(that.page, that.limit).then(res => {
      //   that.loading = false;
      //   //apply();js将一个数组插入另一个数组;
      //   that.collectProductList.push.apply(that.collectProductList, res.data);
      //   that.loadend = res.data.length < that.limit; //判断所有数据是否加载完成；
      //   that.page = that.page + 1;
      // });
    },
    getParameter:function(){
      var self = this;
      var u = navigator.userAgent, app = navigator.appVersion;
      var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
      var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端        
      const win = window;
      var obj={
          "action": "getParameter",
          "content": {
              'method':'setUserInfo',
              'Parameter':{}
          }
      }
      if (isAndroid) {
          App.onAppResponse(JSON.stringify(obj));
      }
      if (isIOS) {
          win.webkit.messageHandlers.onAppResponse.postMessage(JSON.stringify(obj));
      }  
    }, 
    // 嵌入app获取支付状态
    onWebResponse:function(str) {
        var obj=JSON.parse(str);
        var action=obj.action;
        var self=this;
        var content=obj.content;
        if (action=='setUserInfo') {
          let did=content.did;
          let isToken=content.ds_token;
          let expires_time = content.ds_expires_time;
          let isSalt=content.salt;
          if (isToken=='') {
              clearAuthStatus();
          }else{
            // alert(isToken);
            let newTime = Math.round(new Date() / 1000);
            this.$store.commit(
              "LOGIN",
              isToken,
              // dayjs(expires_time) - newTime
            );
          }
      }
    },
    toCart:function(){
      if(this.from1 == 'app'){
        this.$router.push({ path: "/cart?from1=app"});
      }else{
        this.$router.push({ path: "/cart"});
      }
      
    },
    //删除收藏；
    // delCollection: function(index) {
    //   let that = this,
    //     id = that.collectProductList[index].pid,
    //     category = that.collectProductList[index].category;
    //   getCollectDel(id, category).then(function() {
    //     that.$dialog.toast({
    //       mes: "删除收藏成功!",
    //       callback: () => {
    //         that.collectProductList.splice(index, 1);
    //         that.$set(that, "collectProductList", that.collectProductList);
    //       }
    //     });
    //   });
    // }
  }
};
</script>
<style scoped>
.header {
  width: 100%;
  height: 0.8rem;
  background: #fff;
}
.productList3{
  position: absolute;
  width: 100%;
  top: 0;
  bottom: 0;
}
.header img {
  float: right;
  width: 0.4rem;
  margin: 0.2rem 0.12rem 0 0;
}
.header span {
  float: right;
  font-size: 0.28rem;
  color: rgba(0, 0, 0, 0.8);
  line-height: 1;
  padding: 0.26rem 0.3rem 0 0;
}
.noCart {
  margin-top: 0.17rem;
  padding-top: 0.1rem;
}

.noCart .pictrue {
  width: 4rem;
  height: 3rem;
  margin: 46% auto 0.5rem auto;
}

.noCart .pictrue img {
  width: 100%;
  height: 100%;
}
.noCart .txt{
    font-size: .24rem;
    color: rgba(0, 0, 0, .4);
    text-align: center;
    letter-spacing:.01rem;
}
</style>
